package com.hc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.common.bean.PageBean;
import com.hc.common.enums.ResultEnum;
import com.hc.constant.GlobalConstant;
import com.hc.domain.ProductCategory;
import com.hc.ex.GlobalException;
import com.hc.mapper.ProductCategoryMapper;
import com.hc.query.ProductCategoryCondition;
import com.hc.res.ProductCategoryItem;
import com.hc.service.ProductCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hecai
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public int updateBatchSelective(List<ProductCategory> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<ProductCategory> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public PageBean<ProductCategoryItem> queryByCondition(ProductCategoryCondition condition) {
        if (condition == null) {
            throw new GlobalException(ResultEnum.PARAM_ERROR);
        }

        final Long current = condition.getCurrent() == null ? 1 : condition.getCurrent();
        final Integer pageSize = condition.getPageSize() == null ? GlobalConstant.PageSize.DEFALUT_PAGE_SIZE : condition.getPageSize();
        IPage<ProductCategoryItem> page = new Page<>(current, pageSize);

        productCategoryMapper.selectList(page, condition);

        PageBean<ProductCategoryItem> goodsCategoryItemPageBean = PageBean.init(page);
        return goodsCategoryItemPageBean;
    }
}